package com.aishua.fire.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.aishua.fire.common.exp.ValidateException;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@ControllerAdvice
public class GlobalExceptionHandler {
    
 
	/**
	 * 处理登录接口的异常类
	 * @param req
	 * @param e
	 * @return
	 * @throws Exception
	 */
    @ExceptionHandler(value = {ValidateException.class})
    @ResponseBody
    @ResponseStatus(value = HttpStatus.NOT_EXTENDED)
    public void loginExp(HttpServletRequest req, Exception e,HttpServletResponse response) {
        log.warn("---Host: {} invokes url: {} ERROR: {}", req.getRemoteHost(), req.getRequestURL(), e.getMessage());
        /*HttpError httpError = new HttpError();
        httpError.setError("登录失败");
        httpError.setMessage(e.getMessage());
        httpError.setPath("/login");
        httpError.setStatus(510);
        httpError.setTimestamp(System.currentTimeMillis());
        
        return httpError;*/
        try {
			response.sendError(HttpStatus.NOT_EXTENDED.value(), e.getMessage());
		} catch (IOException e1) {
			log.error("{}",e1);
		}
        
    }
 
}

